package com.jims.master.modules.utils;


import com.jims.master.common.utils.SpringContextHolder;
import com.jims.master.modules.operation.entity.OperationStaffAfter;
import com.jims.master.modules.operation.entity.OperationStaffApply;
import com.jims.master.modules.operation.entity.OperationStaffBefore;
import com.jims.master.modules.operation.service.OperationStaffAfterService;
import com.jims.master.modules.operation.service.OperationStaffApplyService;
import com.jims.master.modules.operation.service.OperationStaffBeforeService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.SystemService;

/**
 * ������Ա����
 * @author DT
 */
public class OperationStaffUtils {
    private static OperationStaffApplyService operationStaffApplyService = SpringContextHolder.getBean(OperationStaffApplyService.class);
    private static OperationStaffBeforeService operationStaffBeforeService = SpringContextHolder.getBean(OperationStaffBeforeService.class);
    private static OperationStaffAfterService operationStaffAfterService = SpringContextHolder.getBean(OperationStaffAfterService.class);
    private static SystemService systemService=SpringContextHolder.getBean(SystemService.class);
    /**
     * ����������Ա����
     * @param id
     * @param local
     * @param userIds
     */
    public static void saveStaffApply(String id, String local, String userIds) {
        if (userIds.indexOf(",") != -1) {
            String[] ids = userIds.split(",");
            for (int i = 0; i < ids.length; i++) {
                OperationStaffApply operationStaffApply = new OperationStaffApply();
                operationStaffApply.setScheduleId(id);
                operationStaffApply.setUserId(ids[i]);
                operationStaffApply.setOperationLocal(local);
                operationStaffApplyService.save(operationStaffApply);
            }
        } else {
            OperationStaffApply operationStaffApply = new OperationStaffApply();
            operationStaffApply.setScheduleId(id);
            operationStaffApply.setUserId(userIds);
            operationStaffApply.setOperationLocal(local);
            operationStaffApplyService.save(operationStaffApply);
        }
    }

    /**
     * ����������Ա����
     * @param id
     * @param local
     * @param userIds
     */
    public static void saveStaffBefore(String id, String local, String userIds) {
        if (userIds.indexOf(",") != -1) {
            String[] ids = userIds.split(",");
            for (int i = 0; i < ids.length; i++) {
                OperationStaffBefore operationStaffBefore = new OperationStaffBefore();
                operationStaffBefore.setScheduleId(id);
                operationStaffBefore.setUserId(ids[i]);
                operationStaffBefore.setOperationLocal(local);
                operationStaffBeforeService.save(operationStaffBefore);
            }
        } else {
            OperationStaffBefore operationStaffBefore = new OperationStaffBefore();
            operationStaffBefore.setScheduleId(id);
            operationStaffBefore.setUserId(userIds);
            operationStaffBefore.setOperationLocal(local);
            operationStaffBeforeService.save(operationStaffBefore);
        }
    }

    /**
     * 术后人员登记
     * @param id
     * @param local
     * @param userIds
     */
    public static void saveStaffAfter(String id, String local, String userIds) {
        if (userIds.indexOf(",") != -1) {
            String[] ids = userIds.split(",");
            for (int i = 0; i < ids.length; i++) {
                OperationStaffAfter operationStaffAfter = new OperationStaffAfter();
                operationStaffAfter.setScheduleId(id);
                operationStaffAfter.setUserId(ids[i]);
                operationStaffAfter.setOperationLocal(local);
                operationStaffAfterService.save(operationStaffAfter);
            }
        } else {
            OperationStaffAfter operationStaffAfter = new OperationStaffAfter();
            operationStaffAfter.setScheduleId(id);
            operationStaffAfter.setUserId(userIds);
            operationStaffAfter.setOperationLocal(local);
            operationStaffAfterService.save(operationStaffAfter);
        }
    }

    public static OperationStaffApply getStaffApplyNames(OperationStaffApply operationStaffApply){
        String userIds=operationStaffApply.getUserIds();
        if(userIds.indexOf(",")!=-1){
            String[] ids = operationStaffApply.getUserIds().split(",");
            String[] names=new String[ids.length];
            for (int i = 0; i < ids.length; i++) {
                User user=systemService.getUser(ids[i]);
                names[i]=user.getName();
            }
            StringBuffer strNames=new StringBuffer();
            for(int i=0;i<names.length;i++){
                strNames.append(names[i]);
                if(i<names.length-1){
                    strNames.append(",");
                }
            }
            operationStaffApply.setUserNames(strNames.toString());
        }
        else {
            User user = systemService.getUser(operationStaffApply.getUserId());
            operationStaffApply.setUserNames(user.getName());

        }
        return operationStaffApply;
    }
    public static OperationStaffBefore getStaffBeforeNames(OperationStaffBefore operationStaffBefore){
        String userIds=operationStaffBefore.getUserIds();
        if(userIds.indexOf(",")!=-1){
            String[] ids = operationStaffBefore.getUserIds().split(",");
            String[] names=new String[ids.length];
            for (int i = 0; i < ids.length; i++) {
                User user=systemService.getUser(ids[i]);
                names[i]=user.getName();
            }
            StringBuffer strNames=new StringBuffer();
            for(int i=0;i<names.length;i++){
                strNames.append(names[i]);
                if(i<names.length-1){
                    strNames.append(",");
                }
            }
            operationStaffBefore.setUserNames(strNames.toString());
        }
        else {
            User user = systemService.getUser(operationStaffBefore.getUserId());
            operationStaffBefore.setUserNames(user.getName());

        }
        return operationStaffBefore;
    }

    public static OperationStaffAfter getStaffAfterNames(OperationStaffAfter operationStaffAfter){
        String userIds=operationStaffAfter.getUserIds();
        if(userIds.indexOf(",")!=-1){
            String[] ids = operationStaffAfter.getUserIds().split(",");
            String[] names=new String[ids.length];
            for (int i = 0; i < ids.length; i++) {
                User user=systemService.getUser(ids[i]);
                names[i]=user.getName();
            }
            StringBuffer strNames=new StringBuffer();
            for(int i=0;i<names.length;i++){
                strNames.append(names[i]);
                if(i<names.length-1){
                    strNames.append(",");
                }
            }
            operationStaffAfter.setUserNames(strNames.toString());
        }
        else {
            User user = systemService.getUser(operationStaffAfter.getUserId());
            operationStaffAfter.setUserNames(user.getName());

        }
        return operationStaffAfter;
    }
}